﻿package src.view.ui 
{
	import flash.display.BlendMode;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import src.event.menuEvent;
	
	/**
	 * 顶部下拉菜单
	 * @author	MeatHill
	 * @version	0.1(2009-08-01)
	 */
	public class topBarMenuView extends Sprite
	{
		private const _WIDTH:uint = 280;
		private const _HEIGHT:uint = 20;
		private const _SPACE:uint = 5;
		private const _NORMAL_TF:TextFormat = new TextFormat(null, 14, 0x336699, true, null, null, null, null, "center");
		
		private var _data:XMLList;
		private var _icon_container_mc:Sprite;
		private var _icon_arr:Array;
		private var _icon_loaded_uint:uint = 0;
		private var _icon_loader:Loader;
		private var _ori_height_uint:uint = 0;
		private var _show_rect:Rectangle;
		
		public function topBarMenuView(xlist:XMLList) 
		{
			_data = xlist;
			_ori_height_uint = _data.length() * _HEIGHT + _SPACE * 2;
			
			init();
		}
		
		/**************
		 * properties
		 * ***********/
		public function set show_height(num:uint):void {
			_show_rect.y = _ori_height_uint - num;
			_show_rect.height = num;
			scrollRect = _show_rect;
		}
		public function get show_height():uint {
			return _show_rect.height;
		}
		public function get total_height():uint {
			return _ori_height_uint;
		}
		
		/**************
		 * functions
		 * ***********/
		private function init():void {
			// 先画背景
			graphics.beginFill(0xCCD2DE, .4);
			graphics.drawRect(0, 0, _WIDTH, _ori_height_uint);
			graphics.endFill();
			
			_show_rect = new Rectangle(0, 0, _WIDTH, _ori_height_uint);
			
			// 设置按钮
			var _counter:uint = 0;
			for each (var _item:XML in _data) {
				//trace(_item.toString());
				var _txt:TextField = new TextField();
				_txt.width = 250;
				_txt.height = _HEIGHT;
				_txt.x = 20 + _SPACE;
				_txt.y = _SPACE + _counter * (_HEIGHT + _SPACE);
				_txt.defaultTextFormat = _NORMAL_TF;
				_txt.mouseEnabled = false;
				_txt.blendMode = BlendMode.INVERT;
				_txt.text = _item.toString();
				addChild(_txt);
				var _btn:Sprite = new Sprite();
				_btn.x = _SPACE;
				_btn.y = _SPACE + _counter * (_HEIGHT + _SPACE);
				_btn.addEventListener(MouseEvent.ROLL_OVER, mouseon_handler);
				_btn.addEventListener(MouseEvent.ROLL_OUT, mouseout_handler);
				_btn.addEventListener(MouseEvent.CLICK, click_handler);
				mouseout_handler(null, _btn);
				addChild(_btn);
				
				if (_item.@icon) {
					if (!_icon_arr) {
						_icon_arr = new Array();
					}
					_icon_arr.push( { index:_counter, src:_item.@icon } );
				}
        _counter++;
			}
			if (_icon_arr) {
				_icon_container_mc = new Sprite();
				addChild(_icon_container_mc);
				load_icons();
			}
		}
		private function load_icons(evt:Event = null):void {
			if (_icon_loaded_uint > 0) {
				_icon_loader.x = _SPACE + 5;
				_icon_loader.y = _icon_arr[_icon_loaded_uint - 1].index * _HEIGHT + 15;
				_icon_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, load_icons);
				_icon_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, load_icons_failed);
				_icon_container_mc.addChild(_icon_loader);
			}
			if (_icon_loaded_uint < _icon_arr.length) {
				_icon_loader = new Loader();
				_icon_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, load_icons);
				_icon_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, load_icons_failed);
				_icon_loader.load(new URLRequest(_icon_arr[_icon_loaded_uint++].src));
			}
		}
		private function load_icons_failed(evt:IOErrorEvent):void {
			
		}
		private function mouseon_handler(evt:MouseEvent):void {
			var _target:Sprite = Sprite(evt.currentTarget);
			_target.graphics.clear();
			_target.graphics.beginFill(0x99ccff, .3);
			_target.graphics.drawRect(0, 0, _WIDTH - _SPACE * 2, _HEIGHT);
			_target.graphics.endFill();
		}
		private function mouseout_handler(evt:MouseEvent = null, mc:Sprite = null):void { 
			if (evt && !mc) {
				mc = Sprite(evt.currentTarget);
			}
			mc.graphics.clear();
			mc.graphics.beginFill(0x000000, 0);
			mc.graphics.drawRect(0, 0, _WIDTH - _SPACE * 2, _HEIGHT);
			mc.graphics.endFill();
		}
		private function click_handler(evt:MouseEvent):void {
			var _index = (getChildIndex(Sprite(evt.currentTarget)) - 1) / 2;
			var _evt:menuEvent = new menuEvent(menuEvent.SHOW_DETAIL);
			_evt.command = _data[_index].@type;
			_evt.param = { src:_data[_index].@src, width:_data[_index].@width, height:_data[_index].@height, target:_data[_index].@target };
			_evt.name = _data[_index].toString();
			dispatchEvent(_evt);
		}
	}
}